package com.dh.weibo.dao;

import com.dh.weibo.pojo.Comment;
import com.dh.weibo.pojo.WeiBo;

import java.util.ArrayList;
import java.util.List;

public class CommentDao {
    private List<Comment> comments = new ArrayList<>();


    /**
     * 评论
     *
     * @param comment
     */
    public void comment(Comment comment) {
        comments.add(comment);
    }

    public List<Comment> getComments() {
        return comments;
    }

    /**
     * 根据微博ID删除对应的评论
     *
     * @param id
     */
    public void deleteCommentsByWeiBoID(String id) {
//        for (Comment comment : comments) {//本质就迭代器
//            if (comment.getId().equals(id)) {
//                comments.remove(comment);
//            }
//        }
        for (int i = 0; i < comments.size(); i++) {
            if (comments.get(i).getId().equals(id)) {
                comments.remove(i);
            }
        }
    }

    /**
     * 用ID查找一条评论
     *
     * @param id
     * @return
     */
    public Comment getCommentByID(String id) {
        for (Comment comment : comments) {
            if (comment.getCid().equals(id)) {
                return comment;
            }
        }
        return null;
    }


    /**
     * 根据ID删除对应的评论
     *
     * @param cid
     */
    public void deleteCommentById(String cid) {
        for (int i = 0; i < comments.size(); i++) {
            if (comments.get(i).getCid().equals(cid)) {
                comments.remove(i);// 根据索引删除
            }
        }
    }

    /**
     * 根据微博ID获取对应的评论
     *
     * @param id 微博ID
     */
    public List<Comment> getCommentsByWeiBoID(String id) {
        List<Comment> commentList = new ArrayList<>();
        for (int i = 0; i < comments.size(); i++) {
//            System.out.println(comments.get(i).getId()+"-----"+id);
            if (comments.get(i).getId().equals(id)) {
                System.out.println(comments.get(i).getId());
                commentList.add(comments.get(i));
            }
        }
        return commentList;
    }
}
